<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<el-form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="100px"
				class="demo-ruleForm">
				<el-form-item label="" prop="" v-if="fristBools">
					<view class="flex aic jcsb" style="line-height: 20px;">
						<view class="blue_btn_p" style="width: calc(100%);">
							<text class="gray fonts12 ">为保教育经历完整，首次录入需录入三段教育经历</text>
						</view>
					</view>
				</el-form-item>
				<el-form-item label="" prop="">
					<view class="flex aic jcsb" style="line-height: 20px;">
						<view class="blue_btn_p" style="width: calc(100%);">
							<text class="gray fonts12 ">若最高学历非全日制，则还需填写至全日制学历</text>
						</view>
					</view>
				</el-form-item>
				<el-form-item label="学校名称" prop="schoolName">
					<el-input v-model="ruleForm.schoolName" placeholder="请输入学校名称"></el-input>
				</el-form-item>
				<el-form-item label="在校时间" prop="">
					<view class="flex">
						<el-form-item label="" prop="experienceStartTime">
							<el-date-picker type="date" placeholder="开始时间" format="yyyy-MM-dd"
								value-format="yyyy-MM-dd HH:mm:ss" v-model="ruleForm.experienceStartTime"
								style="width: 100%;">
							</el-date-picker>
						</el-form-item>
						<text style="margin: 0px 10px 0px 10px;">-</text>
						<el-form-item label="" prop="experienceEndTime">
							<el-date-picker type="date" placeholder="结束时间" format="yyyy-MM-dd"
								value-format="yyyy-MM-dd HH:mm:ss" v-model="ruleForm.experienceEndTime"
								style="width: 100%;">
							</el-date-picker>
						</el-form-item>
					</view>
				</el-form-item>
				<el-form-item label="学历" prop="educationLevel">
					<el-select v-model="ruleForm.educationLevel" placeholder="请选择学历" style="width: 100%;">
						<el-option v-for="item in educationLevelOption" :label="item.dictLabel" :value="item.dictValue"
							v-if="item.dictValue!='-1'">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="是否是全日制" prop="isFullTimeEducation">
					<el-select v-model="ruleForm.isFullTimeEducation" placeholder="请选择是否全日制"
						style="width: 350px;">
						<el-option v-for="item in sys_yes_noOption" :label="item.dictLabel" :value="item.dictValue">
						</el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="学信网编码" prop="xuexinCoding">
					<el-input v-model="ruleForm.xuexinCoding" placeholder="请输入学信网编码"></el-input>
				</el-form-item> -->
				<el-form-item label="所学专业" prop="">
					<el-input v-model="ruleForm.specialFieldName" placeholder="请输入所学专业"></el-input>
				</el-form-item>
				<!-- <el-form-item label="担任职位" prop="">
					<el-input v-model="ruleForm.positionName" placeholder="请输入担任职位"></el-input>
				</el-form-item> -->
				<!-- <el-form-item label="专业描述" prop="">
					<el-input type="textarea" v-model="ruleForm.specialFieldDescription" rows="4" placeholder="请输入专业描述">
					</el-input>
				</el-form-item> -->
				<el-form-item label="此学历的学信网截图" prop="">
					<UploadImageList :data="upLoadImages" @uploadImageClick="uploadImageClick_" />
					<!-- <view class="flex aic jcsb" style="line-height: 20px;">
						<el-button class="blue_btn" style="" type="primary">选择上传文件</el-button>
						<view class="blue_btn_p" style="width: calc(100% - 135px);">
							<text class="gray fonts12 ">请上传1寸照，文件大小不超过2M，支持jpg、jpeg、png格式</text>
						</view>
					</view> -->
				</el-form-item>
			</el-form>
		</view>
		<view class="position_footer flex aic jcsb">
			<!-- <el-button class="blue_btn" style="width: 100%;" type="primary" @click="goon('ruleForm')"
				v-if="fristBools&&array.length<2">继续添加</el-button> -->
			<el-button class="blue_btn" style="width: 100%;" type="primary"  @click="submitForm('ruleForm')">保存
			</el-button>
			<el-button class="" style="width: 100%;" type="danger" @click="deleteFun()" v-if="type == 0">删除
			</el-button>
		</view>
	</view>
</template>

<script>
	import {
		editResume
	} from "@/api/my.js"
	import UploadImageList from "@/pages/components/UploadImageList.vue"
	export default {
		components: {
			UploadImageList
		},
		data() {
			const checkPath = (rule, value, callback) => {
				if (value) {
					this.ruleForm.xuexinCoding = value.replace(/[\u4E00-\u9FA5]/g, "");

				}
				callback();
				// 一定要执行回调函数
				// 否则不起效果

			};
			var validateTime = (rule, value, callback) => {
				if (!this.ruleForm.experienceStartTime) {
					callback(new Error("请选择开始时间！"))
				} else if (Date.parse(this.ruleForm.experienceStartTime) >= Date.parse(value)) {
					callback(new Error("结束时间必须大于开始时间！"))
				} else {
					callback();
				}
			};
			return {
				upLoadImages: {
					tip: "（最多上传10张，单张图片大小不超过2M，支持jpg、jpeg、png格式）",
					urls: "",
					limit: 10
				},
				ruleForm: {},
				rules: {
					isFullTimeEducation: [{
						required: true,
						message: '请选择是否全日制',
						trigger: 'change'
					}],
					schoolName: [{
						required: true,
						message: '请输入学校名称',
						trigger: 'blur'
					}],
					experienceStartTime: [{
						required: true,
						message: '请输入在校开始时间',
						trigger: 'blur'
					}],
					experienceEndTime: [{
						required: true,
						message: '请输入在校结束时间',
						trigger: 'blur'
					}, {
						validator: validateTime,
						trigger: ['blur', 'change']
					}, ],
					educationLevel: [{
						required: true,
						message: '请选择学历',
						trigger: 'blur'
					}],
					xuexinCoding: [{
						required: false,
						message: '请输入学信网编码',
						trigger: 'blur'
					}, {
						validator: checkPath,
						trigger: "change"
					}],
					specialFieldName: [{
						required: true,
						message: '请输入专业名称',
						trigger: 'blur'
					}],
				},
				array: [],
				educationLevelOption: [],
				fristBools: false,
				sys_yes_noOption:[]

			};
		},
		onLoad(options) {
			this.educationLevelOption = uni.getStorageSync("education")
			this.sys_yes_noOption=uni.getStorageSync("sys_yes_no")
			this.datas = JSON.parse(options.data)
			this.type = options.type
			if (this.datas.educationExperiences.length == 0) {
				this.fristBools = true
			}
			if (options.type == 0) { //修改
				this.indexValue = options.index
				this.info(this.datas, options.index)
			} else { //新增	
			}

		},
		methods: {
			info(data, index) {
				console.log("data:", data);
				this.ruleForm = data.educationExperiences[index]
				this.upLoadImages.urls = data.educationExperiences[index].files
			},

			uploadImageClick_(value) {
				console.log("多图片：", value);
				this.ruleForm.ossIds = value.ossIds;
				this.ruleForm.files = value.urls
			},
			deleteFun(){
				let obj = this.datas
				obj.educationExperiences.splice(this.indexValue,1)
				editResume(obj).then(res => {
					uni.showToast({
						title: "已删除",
						icon: 'error'
					})
					if (this.type == 1) {
						uni.navigateBack("-1")
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				})
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						let obj = this.datas
						console.log("数据：", obj);
						if (this.fristBools) {
							this.array.push(this.ruleForm)
							obj.educationExperiences = this.array
						} else {
							if (this.type == 0) {
								obj.educationExperiences[this.indexValue] = this.ruleForm
							} else {
								obj.educationExperiences.push(this.ruleForm)
							}
						}
						editResume(obj)
					} else {
						this.$message({
							message: '请完善信息',
							type: 'error',
							offset: 0.1
						});
						console.log('error submit!!');
						return false;
					}
				});
			},
			goon(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.array.push(this.ruleForm)
						this.ruleForm = {}
						console.log("array:", this.array);
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
